
//
//  LLStatusPictureView.swift
//  新浪微博
//
//  Created by mac on 16/5/28.
//  Copyright © 2016年 mac.com. All rights reserved.
//
import UIKit
import SnapKit
//图片之间的间距
private let pictureMargin: CGFloat = 3
//最大宽度
private let maxWidth = ScreenWidth - 2 * StatusCellMargin
//单张图片的宽度
private let itemWidth = (maxWidth - 2 * pictureMargin) / 3
private let pictureCellId = "pictureCellId"
class LLStatusPictureView: UICollectionView {
    //数据源
    var pictures: [LLPicture]?{
        didSet{
            testLabel.text = "\(pictures?.count ?? 0)"
            let pSize = calculatePictureViewSize()
            self.snp_updateConstraints { (make) in
                make.size.equalTo(pSize)
        }
            reloadData()
        }
    }
    func calculatePictureViewSize() -> CGSize{
        let imageCount = pictures?.count ?? 0
        if imageCount == 0 {
        return CGSizeZero
        }
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        if imageCount == 1 {
            let imageSize = CGSizeMake(100,150)
            layout.itemSize = imageSize
            return imageSize
        }

        if imageCount == 4 {
            let w = 2 * itemWidth + pictureMargin
            return CGSizeMake(w, w)
        }
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        let row = CGFloat((imageCount - 1) / 3) + 1
        let maxHeight = row * itemWidth + (row - 1) * pictureMargin
        return CGSizeMake(maxWidth, maxHeight)
        
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureMargin
        layout.minimumLineSpacing = pictureMargin
        super.init(frame: frame, collectionViewLayout: layout)
        //设置代理
        self.dataSource = self
        //设置不能响应点击状态栏快速回到的 手势
        scrollsToTop = false
        //注册
        backgroundColor = UIColor.whiteColor()
        setupUI()
   registerClass(LLStatusPictureCell.self, forCellWithReuseIdentifier: pictureCellId)
    }
    private func setupUI() {
        addSubview(testLabel)
        //设置约束
        testLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self.snp_center)
        }
    }
    
    private lazy var testLabel: UILabel = UILabel(title: "", font: 30, textColor: UIColor.redColor())
    

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
  }
//MARK: DataSource
extension LLStatusPictureView: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictures?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellId, forIndexPath: indexPath) as! LLStatusPictureCell
//        cell.backgroundColor = randomColor()
        //给cell的模型赋值
        cell.picture = pictures![indexPath.item]
        return cell
    }
}


class LLStatusPictureCell: UICollectionViewCell {
    //给cell 设置模型
    var picture: LLPicture? {
        didSet {
            let url = NSURL(string: picture?.thumbnail_pic ?? "")
            
            imageView.sd_setImageWithURL(url)
        }
    }
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
        backgroundColor = UIColor.whiteColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置界面
    private func setupUI() {
        contentView.addSubview(imageView)
        //设置约束
        imageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //懒加载控件
    private lazy var imageView: UIImageView = {
    let v = UIImageView()
        v.contentMode = .ScaleAspectFill
        
        v.clipsToBounds = true
    return v
    }()
}

